package com.anpy.anlibrary.base

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.view.Window
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.anpy.anlibrary.net.Settings
import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.Job

abstract class LibBaseActivity : AppCompatActivity() {

//    private val loadingView by lazy { createLoadingViewInFrameLayout(this) }

    protected val jobs by lazy { mutableListOf<Job>() }
    protected open var loadingMilliSeconds = 0L
    protected val activityList by lazy { mutableListOf<LibBaseActivity>() }

    @Volatile
    private var showLoadingTimes = 0

    private val loadingProgress by lazy {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Settings.activityStack().add(this)
        ImmersionBar.with(this).statusBarDarkFont(true).init()
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView()
        initData()
        val handler = Handler()
        with(window.decorView) {
            post {
                handler.post {
                    /*this as FrameLayout
                    addView(loadingView)*/
//                    initData()
                }
            }
        }
    }

    abstract fun initView()

    /**
     * 本方法初始化数据
     */
    abstract fun initData()

    /**
     * token丢失时需要要做的事情
     */
    open fun tokenLoseDoing() = Unit

    override fun onDestroy() {
        Settings.activityStack().remove(this)
        super.onDestroy()
    }

    /**
     * 权限检查
     *
     * @param neededPermissions 需要的权限
     * @return 是否全部被允许
     */
    protected open fun checkPermissions(neededPermissions: Array<String>): Boolean {
        if (neededPermissions.isEmpty()) {
            return true
        }
        var allGranted = true
        for (neededPermission in neededPermissions) {
            allGranted = allGranted and (ContextCompat.checkSelfPermission(
                this,
                neededPermission
            ) == PackageManager.PERMISSION_GRANTED)
        }
        return allGranted
    }

    /**
     * 请求权限的回调
     *
     * @param requestCode  请求码
     * @param isAllGranted 是否全部被同意
     */
    abstract fun afterRequestPermission(requestCode: Int, isAllGranted: Boolean)


    private val REQ_PERMISSION_CODE = 0x1000
    private var mGrantedCount = 0

    protected abstract fun onPermissionGranted()

    protected open fun checkPermission(): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val permissions: MutableList<String> = ArrayList()
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
            ) {
                permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.CAMERA
                )
            ) {
                permissions.add(Manifest.permission.CAMERA)
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.RECORD_AUDIO
                )
            ) {
                permissions.add(Manifest.permission.RECORD_AUDIO)
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                )
            ) {
                permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE)
            }
            if (permissions.size != 0) {
                ActivityCompat.requestPermissions(
                    this@LibBaseActivity,
                    permissions.toTypedArray(),
                    REQ_PERMISSION_CODE
                )
                return false
            }
        }
        return true
    }
}